export { debounce } from 'lodash'

export const getRandom = (start: number, end: number): number => {
  //获得指定区间的随机数
  return Math.floor(start + (end + 1 - start) * Math.random())
}

/**
 *
 * @param count //指定数量
 * @param start //指定开始范围
 * @param end //指定结束范围
 * @returns 指定区间指定数量的不重复随机数 Array<number>
 */
//获取指定区间指定数量的不重复随机数
export const getNotRepeatRandomNumbers = (
  count: number,
  start: number,
  end: number
): Array<number> => {
  if (count == end || count > end) {
    console.warn("所要随机数的数量不能大于最大数或者等于最大数!")
    return
  }
  const NotRepeatRandomNumbersArr = []
  while (NotRepeatRandomNumbersArr.length != count) {
    const randomNum = getRandom(start, end)
    const isRepeat = NotRepeatRandomNumbersArr.some(item => item == randomNum)
    if (!isRepeat) {
      NotRepeatRandomNumbersArr.push(randomNum)
    }
  }
  return NotRepeatRandomNumbersArr
}

//洗牌算法数组乱序
export const shuffle = (arr: Array<any>): Array<any> => {
  let result = new Array(),
    randomIndex
  while (arr.length > 0) {
    randomIndex = Math.floor(Math.random() * arr.length)
    result.push(arr[randomIndex])
    arr.splice(randomIndex, 1)
  }
  return result
}

/**
 *
 * @param data
 * @param pageSize
 * @returns 二维数组
 */
export const pagingData = (data: Array<any>, pageSize: number): Array<Array<any>> => {
  let pushArr: Array<any> = []
  let reslutData: Array<Array<any>> | Array<any> = []
  data.forEach((item: any, index: number) => {
    if (index > 0 && index % pageSize == 0) {
      reslutData.push(pushArr)
      pushArr = []
    }
    pushArr.push(item)
  })
  reslutData.push(pushArr)
  return reslutData
}

interface Element {
  requestFullscreen?: any
  mozRequestFullScreen?: any
  webkitRequestFullscreen?: any
  msRequestFullscreen?: any
}

export const fullScreenIn = (el: Element = document.body) => {
  //将传入元素全屏显示 默认全屏body
  if (el.requestFullscreen) {
    el.requestFullscreen() //通用浏览器下
  } else if (el.mozRequestFullScreen) {
    el.mozRequestFullScreen() //火狐下
  } else if (el.webkitRequestFullscreen) {
    el.webkitRequestFullscreen() //谷歌下
  } else if (el.msRequestFullscreen) {
    el.msRequestFullscreen() //safari下
  }
}

interface doc extends Document {
  exitFullScreen?: any
  mozCancelFullScreen?: any
  webkitExitFullscreen?: any
  msExitFullscreen?: any
  fullscreenElement: any
  mozFullScreenElement?: any
  msFullScreenElement?: any
  webkitFullscreenElement?: any
  mozFullScreen?: any
  webkitIsFullScreen?: any
  webkitFullScreen?: any
  msFullScreen?: any
}
export const fullScreenOut = () => {
  //退出全屏
  const doc: doc = document
  if (doc.exitFullScreen) {
    doc.exitFullScreen()
  } else if (doc.mozCancelFullScreen) {
    doc.mozCancelFullScreen()
  } else if (doc.webkitExitFullscreen) {
    doc.webkitExitFullscreen()
  } else if (doc.msExitFullscreen) {
    doc.msExitFullscreen()
  }
}

export const getFullscreenEl = () => {
  //获取当前全屏的元素节点
  const doc: doc = document
  return (
    doc.fullscreenElement ||
    doc.mozFullScreenElement ||
    doc.msFullScreenElement ||
    doc.webkitFullscreenElement ||
    null
  )
}

export const isFullScreen = () => {
  //判断当前是否是全屏
  const doc: doc = document
  if (doc.fullscreen != null) {
    return doc.fullscreen
  } else if (doc.mozFullScreen != null) {
    return doc.mozFullScreen
  } else if (doc.webkitIsFullScreen != null) {
    return doc.webkitIsFullScreen
  } else if (doc.webkitFullScreen != null) {
    return doc.webkitFullScreen
  } else if (doc.msFullScreen != null) {
    return doc.msFullScreen
  }
}

export const toStr = (data: any): string => {
  return Object.prototype.toString.call(data).slice(8, -1)
}

export const throttle = (fn: Function, timeOut: number): Function => {
  let flag: boolean = true
  return (...arg: Array<any>) => {
    if (flag) {
      fn(...arg)
    }
    flag = false
    setTimeout(() => {
      flag = true
    }, timeOut)
  }
}

// export const debounce = (fn: Function, timeOut: number): any => {
//   let timer: any
//   return (...arg: Array<any>) => {
//     clearTimeout(timer)
//     timer = setTimeout(() => {
//       fn(...arg)
//     }, timeOut)
//   }
// }

/**
 * 之前某天到今天经过的天数
 */
export const betweenDateToToday = date => {
  let date1 = new Date(date.replace(/-/g, "/"))
  let date2 = new Date()
  let days = date2.getTime() - date1.getTime()
  return parseInt(days / (1000 * 60 * 60 * 24) + '')
}

export const formatDate = (date) => {
  if (typeof date == 'string') {
    date = Number(date)
  }
  let res = ''
  const dateInstance = new Date(date);
  const Year = dateInstance.getFullYear();
  const Month = dateInstance.getMonth() + 1;
  const Day = dateInstance.getDate();
  const Time = dateInstance.toLocaleTimeString();
  res = `${Year}-${Month
    .toString()
    .padStart(2, "0")}-${Day
      .toString()
      .padStart(2, "0")} ${Time}`;
  return res
}

export const sleep = (timeout: number) => new Promise((res) => setTimeout(res, timeout))


export function pagination<T = any>(pageNo: number, pageSize: number, array: T[]): T[] {
  const offset = (pageNo - 1) * Number(pageSize);
  return offset + Number(pageSize) >= array.length
    ? array.slice(offset, array.length)
    : array.slice(offset, offset + Number(pageSize));
}